<!-- Tan Phat -->
<?php 	echo $skq = '<table width="800px" border="0" cellpadding="0px" cellspacing="0px">
		    <tr>
			<td width="1%" height="29" style="background:url(images/bar_left.gif);background-repeat:no-repeat"></td>
			<td width="99%" style="background-image:url(images/bar_mid.gif);background-repeat:repeat-x">
			<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
			<td width="3%" height="24"><img src="images/bullet_next.png" width="13" height="12" /></td>
			<td width="51%" style="color:#FFF;font-size:18px;font-weight:bold;">Lấy lại mật khẩu </td>
			 <td width="23%" align="right">
			</td>
			<td align="right" width="23%" style="color:#FFF;padding-right:10px;">
			</td>
			</tr></table>    
			</td>
		  </tr>
		  <tr>
			<td colspan="2">';
	?>

<?php																																					    include_once('components/user/class_user.php');
	include_once('library/MailPackage/sendmail.php');
	
	
	//--------------------------------------------//
	define ("MAX_SIZE","1024"); 

	function getExtension($str) {
		 $i = strrpos($str,".");
		 if (!$i) { return ""; }
		 $l = strlen($str) - $i;
		 $ext = substr($str,$i+1,$l);
		 return $ext;
	}
	
	$errors=0;
	
	if(isset($_POST['uploadimg'])) 
	{
		$image=$_FILES['addHinhAnh']['name'];
	
		if ($image) 
		{
			$filename = stripslashes($_FILES['addHinhAnh']['name']);
			$extension = getExtension($filename);
			$extension = strtolower($extension);
		
			if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
			{
				echo '<h1>Không phải file ảnh (các file có hổ trợ: *.jpg,*.jpeg,*.png,*.gif)!</h1>';
				$errors=1;
			}
			else
			{
				$size=filesize($_FILES['addHinhAnh']['tmp_name']);
	
				if ($size > MAX_SIZE*1024)
				{
					echo '<h1>Kích thước quá lớn (size file < 1MB)!</h1>';
					$errors=1;
				}
				move_uploaded_file($_FILES["addHinhAnh"]["tmp_name"],"data/pic_user/" . $_FILES["addHinhAnh"]["name"]);
				$hinhanh = $_FILES['addHinhAnh']['name'] ;
				//echo 'Stored in: ' . '../data/pic_ebook/' . $_FILES['image']['name'];
			}
		}
		else
		{
				$flag = 0 ;
				$smg_img = 'Lỗi khi upload file hình ảnh !';
		}
	}
	if(isset($_POST['Submit']) && !$errors) 
	{
		echo "<h3 align='left' style='color:Blue'>Uploaded ảnh thành công</h3>";
	}

	//--------------------------------------------//
	
	$LoiCode ="";
	$LoiUserName = "";
	$LoiEmail ="";
	
	$tendangnhap= '';
	$matkhau = '';
	$email= '';
	$reemail= '';
	$hoten = '';
	$ngay = date('d');
	$thang = date('m');
	$nam = date('y');
	$diachi = '';
	$dienthoai = 0;		
	$gioitinh = 1;
	$hinhanh = 'nopic1.gif';
	
	$Type = 0;
	if(isset($_REQUEST['Type']))
		$Type = $_REQUEST['Type'];
	
	if ( $Type == 1 )
	{
		$tendangnhap = $_REQUEST["tendangnhap"];
		$matkhau =  $_REQUEST["matkhau"];
		$email = $_REQUEST['email'];
		$hoten = $_REQUEST['hoten'];
		$ngay = $_REQUEST['ngay'];
		$thang = $_REQUEST['thang'];
		$nam = $_REQUEST['nam'];
		$diachi = $_REQUEST['diachi'];
		$dienthoai = $_REQUEST['dienthoai'];
		$gioitinh = $_REQUEST['gioitinh'];
		$hinhanh = $_REQUEST['hinhanh'];
//		$hinhanh = "images/thanhvien/".$hinhanh;

		$code = $_REQUEST["code"];
	
		
		$tendangnhap = strtolower($tendangnhap);
		$flag = 1;	
		$tv_user = new XuLy_User();
		$row_user = $tv_user->KiemTraTonTaiUserName($tendangnhap);

		if($row_user != NULL)
		{
			$LoiUserName = "Tài khoản đã có người sử dụng";
			$flag = 0;
		}
		
		$tv_email = new XuLy_User();
		$row_email = $tv_email->KiemTraTonTaiEmail($email);
		if($row_email != NULL)
		{
			$LoiEmail = "Email đã có người sử dụng";
			$flag = 0;
		}
			
		if(!isset($_SESSION["security_code"]) || $_SESSION["security_code"] != $code)
		{
			$LoiCode ="Kí tự không khớp với hình";
			$flag = 0;
		}
		
		//--------- kiem tra linh vuc ---------------------------
		$IDsLinhVuc = $_REQUEST["IDsKhoa"] ;
		$token = strtok($IDsLinhVuc ,",");
		$arr = array();
		$i=0 ;
		while ($token != false)
		{
			$arr[$i++] = $token ;
			$token = strtok(",");
		}
		if ( count($arr) < 1 )
		{
			$LoiCode ="Chưa chọn lĩnh vực quan tâm";
			$flag = 0;
		}
		//--------- kiem tra linh vuc ----------------------------
		if($flag == 1)
		{
			$matkhau = $_REQUEST['matkhau'];
			$matkhau = md5($matkhau) ;
			
			$stringdate = $nam.'-'.$thang.'-'.$ngay ;
			$ngaysinh = date($stringdate) ;
			
			$md5_hash = md5(rand(0,999)); // phat sinh ma kich hoat	
			$activecode = substr($md5_hash, 15, 20); 
	
			
			$tv_add = new XuLy_User();
			$mauser = $tv_add->themmoi($tendangnhap,$matkhau,$email,$hoten,$diachi,$dienthoai,$gioitinh,$ngaysinh,$hinhanh,$activecode);
			
			if($mauser != null)
			{
				//------ send mail -------------------------------------
				$kqsend = sendmail($email , $mauser ,$activecode);
				if($kqsend == 0)
				{
					echo "<div style='color:red'>Không gửi được mail ! ! !</div>";
					exit();
				}	
				//------------up date linh vực------------------------			
				for ( $i=0 ; $i < count($arr) ; $i++ )
				{
					$tv_addlv = new XuLy_User();
					$ID = $arr[$i] ;
					$addlinhvuc = $tv_addlv->themlinhvuc($mauser,$ID);
					if($addlinhvuc == null)
					{
						echo "<div style='color:red'>Thêm Lĩnh vực không thành công ! ! !</div>";
						exit();
					}
					
				}	
				//-----------------------------//
				
				echo "<meta http-equiv='refresh' content='0;url=index.php?com=user&fun=loginsuccess&type=1&$kqsend' />";
			}
			else
			{
				echo "<div style='color:red'>Đăng ký thất bại !</div>";
			}
		}	
	}
	
	?>
    <div align="left">
    <?php 
		echo "<div style='color:red' align='left'> 
			 $LoiUserName
   			 $LoiEmail
     		 $LoiCode 
			 </div>"; 
	?>
    
	<table class='sample' align="center">
		  <tr>
			<td>Tên Đăng Nhập</td>
			<td><input id='addTenDangNhap' name='addTenDangNhap' type='text' value='<?php echo $tendangnhap ?>' onkeyup='ktuser()';/>(*)</td>
			<td><div id='loitendangnhap'></div></td>
		  </tr>
		   <tr>
			<td>&nbsp;</td>
			<td colspan='2' align='center'>Tên đăng nhập phải gồm kí tự a->z,0->9.</td>
		  </tr>
		   <tr>
			<td>Mật Khẩu</td>
			<td><input id='addMatKhau' name='addMatKhau' type='password' onkeyup='ktmatkhau1()';/>(*)</td>
			<td><div id='loimatkhau'></div></td>
		  </tr>
		  <tr>
			<td>Nhắc lại Mật Khẩu</td>
			<td><input id='addReMatKhau' name='addReMatKhau' type='password' onkeyup='ktmatkhau2()';/>(*)</td>
			<td><div id='loairematkhau'></div></td>
		  </tr>
		   <tr>
			<td>&nbsp;</td>
			<td colspan='2' align='center'>Mật khẩu phải nhiều hơn 6 kí tự</td>
		  </tr>
		  <tr>
			<td>Email</td>
			<td><input id='addEmail' name='addEmail' type='text' value='<?php echo $email ?>' onkeyup='ktemailhople();'/>(*)</td>
			<td><div id='loiemail'></div></td>
		  </tr>
		  <tr>
			<td>Nhắc lại Email</td>
			<td><input id='addReEmail' name='addReEmail' type='text' value='<?php echo $reemail ?>' onkeyup='ktkhopemail();'/>(*)</td>
			<td><div id='loireemail'></div></td>
		  </tr>
		  <tr>
			<td colspan='3'>&nbsp;</td>
		  </tr>
		  <tr>
			<td>Họ tên</td>
			<td><input id='addHoTen' name='addHoTen' type='text' value='<?php echo $hoten ?>' onkeyup='kthoten();'/>(*)</td>
			<td><div id='loihoten'></div></td>
		  </tr>
		  <tr>
			<td>Địa Chỉ</td>
			<td><input id='addDiaChi' name='addDiaChi' type='text' value='<?php echo $diachi ?>'/></td>
			<td>&nbsp;</td>
		  </tr>		  
		 <tr>
			<td>Điện Thoại</td>
			<td><input id='addDienThoai' name='addDienThoai' type='text' value='<?php echo $dienthoai ?>' onkeyup='ktdienthoai();'/></td>
			<td><div id='loidienthoai'></div></td>
		  </tr>
		   <tr>
			<td>Giới tính</td>
			
				<td><input id='addGioiTinh' name='addGioiTinh' type='radio' checked='checked' value='Nam'/>Nam &nbsp;
				<input id='addGioiTinh' name='addGioiTinh' type='radio' value='Nu'/>	Nữ</td> 
		<td>&nbsp;</td>
		  </tr>
		   <tr>
			<td>Ngày sinh</td>
            <?php 
				echo "<td>
				<select name='ngay' id='ngay' >";
					for($i = 1 ; $i<=31 ; $i++)
					{
						if($ngay == $i )
							echo "<option value='$i' selected='selected'>$i</option>";
						else
							echo "<option value='$i'>$i</option>";
					}
				echo "</select>";
				echo "&nbsp;&nbsp;";
				echo "<select name='thang' id='thang' >";
					for( $i = 1 ; $i<=12 ; $i++)
					{
						if($thang == $i )
							echo "<option value='$i' selected='selected'>$i</option>";
						else
							echo "<option value='$i'>$i</option>";
					}					
				echo "</select>";
				echo "&nbsp;&nbsp;";
				echo "<select name='nam' id='nam' >";	
					for($i = date('Y') ; $i>1929 ; $i--)
					{
						if($nam == $i )
							echo "<option value='$i' selected='selected'>$i</option>";
						else
							echo "<option value='$i'>$i</option>";
					}
					//<img src="images/static.jpg"/>
					//
				?>
				</select>
				</td>
			<td>&nbsp;</td>
		  </tr>
		  <tr>
			<td>Hình Ảnh</td>
            <form name='newad' method='post' enctype='multipart/form-data'  action=''>
            <td>
			<input id='addHinhAnh' name='addHinhAnh' type='file' value='$hinhanh'/>
            <input name='uploadimg' type='submit' value='Upload image'></td>
            </form>
			<td>&nbsp;</td>
		  </tr>
		  
		  <tr><td colspan='3'>&nbsp;</td></tr>
			<tr>
				<td></td>
				<td><img src="components/user/captcha.php" id='captcha' alt='captcha'/>
				</td>
                <td>&nbsp;</td>
			</tr>
			<tr>	
				<td></td>
				<td><label for='security_code'><strong></p>Vui lòng điền các kí tự trong hình trên</strong></label></td>
                <td>&nbsp;</td>
			</tr>
			<tr>
				<td></td>
				<td><input type='text' id='security_code' name='security_code' autocomplete='off' onkeyup='KiemTraCode()'/>(*)</td>
				<td><div id='ErrorCode'></div></td>
			</tr>		
		</table>	
        
       <h3 align="center">Lĩnh vực quan tâm</h3>
       <table class='sample' align="center">
        <?php 
		$le = new XuLy_User();
		$kq = $le->linhvucquantam();
		foreach ($kq as $row)
		{
			$MaDanhMuc = $row['MaDanhMuc'];
			$TenDanhMuc= $row['TenDanhMuc'];
		echo "
			<tr>
        	<td><input type='checkbox' name='checkdangky[]' value='$MaDanhMuc' />$TenDanhMuc</td>
			</tr>
		";
		}
		?>
        </table>
	<div align="center">
	<br />
    <a href=index.php?com=user&fun=register><input type='button' name='New' value='Làm lại' /></a>
	&nbsp;&nbsp;&nbsp;<input type='submit' name='Add' value='Đăng ký' onclick='kiemtradangki()' />
    </div>
    
    	<?php 
		echo '</td></tr></table></form>' ;
	?>
	<div style="height:50px;width:100%" align="center">&nbsp;</div>